package com.easemob.cases.ui;

import android.content.Context;
import android.text.Spannable;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.czy.daishusys.R;
import com.czy.daishusys.utils.JsonUtil;
import com.czy.daishusys.utils.MyLog;

import com.czy.daishusys.utils.glide.GlideUtils;
import com.easemob.cases.MessageHelper;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.easeui.widget.EaseChatMessageList;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;


/**
 * Created by King on 2016/12/13 0013.
 */
public class ChatRowPackage extends EaseChatRow {
    private TextView contentView;
    private ImageView packageLogo;
    public ChatRowPackage(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    public void setUpView(EMMessage message, int position, EaseChatMessageList.MessageListItemClickListener itemClickListener) {
        super.setUpView(message, position, itemClickListener);
    }

    @Override
    protected void setMessageSendCallback() {
        super.setMessageSendCallback();
    }

    @Override
    protected void setMessageReceiveCallback() {
        super.setMessageReceiveCallback();
    }


    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_package : R.layout.ease_row_received_package, this);
    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        packageLogo = (ImageView) findViewById(R.id.iv_package_logo);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
        // 设置内容
        contentView.setText(span, TextView.BufferType.SPANNABLE);
        String type = message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE,null);
        MyLog.e("yang","type=="+type);
        String  data  = message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null);
        MyLog.e("yang","data=="+data);
        //Log.i("King", "MESSAGE_EXT_CUSTOM_TYPE:" + message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE,null));
        if("goods".equals(type) || "package".equals(type)){
            try {
                Log.i("King", JsonUtil.getJsonValuesString(data,"logo"));
                GlideUtils.loadImage(context, JsonUtil.getJsonValuesString(data,"logo"), packageLogo,null, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        packageLogo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBubbleClick();
            }
        });
    }


    @Override
    protected void onBubbleClick() {
        // TODO 跳转至门店套餐详情页购买
//        try {
//            String type = message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE,null);
//            String  data  = message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null);
//            Intent intent  = new Intent();
//            //MyLog.e("yang","data=="+data);
//            switch (type){
//                case "package":
//                    int servicePackageId = JsonUtil.getJsonValuesInt(data,"servicePackageId");
//                    int pushPackageId = JsonUtil.getJsonValuesInt(data,"pushPackageId");
//                    intent.setClass(context, PushPackageCardDetailActivity.class);
//                    intent.putExtra("servicePackageId",servicePackageId);
//                    intent.putExtra("pushPackageId",pushPackageId);
//                    break;
//                case "goods":
//                    intent.setClass(context, GoodsDetailActivity.class);
//                    intent.putExtra("goodsId", JsonUtil.getJsonValuesInt(data,"goodsId"));
//                    break;
//            }
//            //MyLog.e("yang","servicePackageId=="+servicePackageId+"\npushPackageId==="+pushPackageId);
//            context.startActivity(intent);
//        }catch (Exception e){
//            PhoneUtils.ShowToastMessage(context,"参数异常");
//        }

    }


}
